<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="人员名称" prop="vUserName">
        <el-input
          v-model="queryParams.vUserName"
          placeholder="请输入人员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员角色" prop="vRoleName">
        <el-input
          v-model="queryParams.vRoleName"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="buy0010List"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column label="人员名称" align="center" prop="vUserName" />
      <el-table-column label="人员角色" align="center" prop="vRoleName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleView(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-drawer
      :title="title"
      :before-close="handleClose"
      :visible.sync="open"
      size="75%"
      ref="drawer"
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleTableAdd"
                  >新增</el-button
                >
              </el-col>
            </el-row>
            <el-table :data="itemList" height="450px">
              <el-table-column
                type="index"
                label="序号"
                align="center"
                width="50"
              ></el-table-column>
              <el-table-column
                prop="vBrandName"
                label="品牌"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.vBrandName"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column
                prop="isTemporary"
                label="类型"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <!-- <el-input
                    size="small"
                    v-model="scope.row.vKind"
                    @change="handleEdit(scope.$index, scope.row)"
                  >
                </el-input> -->
                  <el-radio v-model="scope.row.isTemporary" label="1"
                    >临时有效</el-radio
                  >
                </template>
              </el-table-column>
              <el-table-column
                prop="temporaryDate"
                label="有效截止日"
                align="center"
                min-width="100"
              >
                <template slot-scope="scope">
                  <el-date-picker
                    v-model="scope.row.temporaryDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </template>
              </el-table-column>
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="itemDelete(scope.$index, scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="saveButton">保存</el-button>
              <el-button @click="cancelDrawer">取消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, getInfo, save, delItem } from "@/api/buy/buy0010";

export default {
  name: "buy0010",

  data() {
    return {
      // 遮罩层
      loading: true,
      kucunOpen: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存信息表格数据
      buy0010List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        vUserName: null,
        vRoleName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      itemList: [],
      userId: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存信息列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.buy0010List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleView(row) {
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.title = "查看";
        this.open = true;
        this.itemList = response.data;
        this.userId = row.id;
      });
    },
    cancelDrawer() {
      this.open = false;
      this.getList;
    },
    /** 提交按钮 */
    saveButton() {
      save(this.itemList).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
    // 添加行
    handleTableAdd() {
      var _this = this;

      let list = {
        id: null,
        userId: this.userId,
        vBrandName: "",
        status: 1,
        isTemporary: "0",
        temporaryDate: null,
      };

      _this.itemList.push(list);
    },

    itemDelete(index, row) {
      var _this = this;
      this.$modal
        .confirm("是否确认删除该条数据项？")
        .then(function () {
          if (
            row.id === "" ||
            row.id === null ||
            row.id === undefined ||
            row.id === 0
          ) {
            _this.itemList.splice(index, 1);
          } else {
            delItem(row).then((response) => {
              _this.itemList.splice(index, 1);
            });
          }
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
.xin-main {
  position: relative;
  height: 100%;
  overflow: hidden;
}
.xin-content {
  overflow-y: scroll;
  box-sizing: border-box;
  overflow-x: hidden;
}
</style>
